<template>
	<view>
		<view class="nav-box">
			<view class="nav">
				<image class="nav-image" src="../../../static/images/icons/back.svg" @click="back"></image>
				<view class="nav-title">修改头像</view>
				<button class="nav-button" @click="save">保存</button>
			</view>
			<view class="middle">
				<image src="../../../static/images/user/top.png" mode=""></image>
			</view>
			<image class="avatar" :src="require('../../../static/images/avatar/p' + avatarId + '.png')" mode=""></image>
		</view>
		
		<view class="card-box">
			
			<view class="avatar-list">
				<view class="item" v-for="i in 88">
					<image v-if="avatarId == i" :src="require('../../../static/images/avatar/p' + i + '.png')" mode="" @click="selectAvatar(i)" :style="styleSelectd"></image>
					<image v-if="avatarId != i" :src="require('../../../static/images/avatar/p' + i + '.png')" mode="" @click="selectAvatar(i)" :style="styleDefault"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {updateUser} from "../../../static/js/User.js"
	export default {
		data() {
			return {
				"avatarId":0,
				"userInfo":{},
				"styleSelectd":"border: 5rpx solid #c8eb21;",
				"styleDefault":"border: 5rpx solid #efefef;"
			}
		},
		onShow() {
			this.userInfo = uni.getStorageSync("userInfo")
			var avatarPath = this.userInfo.avatarPath
			this.avatarId = parseInt(avatarPath.split("/p")[1].split(".")[0])
		},
		methods: {
			back(){
				uni.navigateBack({
					url: '/pages/user/editInfo'
				})
			},
			selectAvatar(avatarId){
				console.log("------selectAvatarId:",avatarId)
				this.avatarId = avatarId
				this.userInfo.avatarPath = "../../static/images/avatar/p" + avatarId + ".png"
			},
			save(){
				updateUser(this.userInfo).then(res=>{
					uni.setStorageSync("userInfo",this.userInfo)
					uni.navigateBack({url: '/pages/user/editInfo'})
				})
			}
		}
	}
</script>

<style>
	@import url("../../../static/css/base.css");
	.nav-button{
		margin-right: 40rpx;
	}
	.middle{
		width: 100%;
		padding:0;
		background-color: #efefef;
		text-align: center;
	}
	.middle image{
		position: relative;
		top: 15rpx;
		width: 92%;
		margin: 0 4%;
		height: 300rpx;
		border-top-left-radius: 50rpx;
		border-top-right-radius: 50rpx;
	}
	.avatar{
		width: 220rpx;
		height: 220rpx;
		position: relative;
		border-radius: 50%;
		top: -250rpx;
		z-index: 1;
	}
	.card-box{
		position: relative;
		width: 92%;
		margin: 0 4%;
	}
	.avatar-list{
		margin-top: 500rpx;
		position: relative;
		background-color: white;
		display: flex;
		justify-content: space-around;
		flex-wrap: wrap; 
	}
	.item image{
		position: relative;
		width: 120rpx;
		height: 120rpx;
		margin: 10rpx 20rpx;
		border: 5px solid #efefef;
		border-radius: 50%;
		z-index: 10;
		
	}
</style>
